from django.contrib.auth.hashers import make_password
from django.http import HttpResponse
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import User


class ShouView(APIView):
    def get(self, request):
        return HttpResponse('这个是首页')


# 注册接口
class Register(APIView):
    def get(self, request, *args):
        # 接收参数
        username = request.GET.get('username', None)
        password = request.GET.get('password', None)

        # 排重操作
        user = User.objects.filter(username=username).first()

        if user:
            return Response({'code': 403, 'msg': '该用户名已存在'})
        #
        user = User(username=username, password=make_password(password))


class WeiboView(APIView):
    def get(self, request):
        # 获取传递来的code
        code = request.GET.get('code')
        print('code:%s' % code)

        data = {
            "client_id": 2194689836,
            "client_secret": "c3fe7719d4db445287b47dff15bdb999",
            "grant_type": "",
            "code": code,
            "redirect_url": "http:127.0.0.1:8080/weibo"
        }
